const moment = require('moment');
const jwt = require('jsonwebtoken');
const utilTool = require('../../util');
const $config = require('../../config');

module.exports = {
  userList: async (ctx, next) => {
    $config.documents = 'site' // 切换文档集合
    const token = ctx.request.header.token;
    await jwt.verify(token, $config.tokenSecret, async (err, resultToken) => { // resultToken 解析token值
      if (err) {  //  时间失效的时候/ 伪造的token
        ctx.body = {
          success: false,
          result: '验证已过期！',
          ...err
        };
        return;
      }
      const {userName, skip, limit} = ctx.request.query;
      const resultData = {};
      resultData.total = await utilTool.count({});
      resultData.tableData = await utilTool.find({}, {passWord: 0}, skip, limit, {});
      resultData.tableColumns = [
        {
          type: 'index',
          title: '序号',
          width: 60,
          align: 'center',
        },
        {
          title: '姓名',
          slot: 'userName',
          key: 'userName',
        },
        {
          title: '密码',
          slot: 'passWord',
          key: 'passWord',
        },
        {
          title: '年龄',
          slot: 'age',
          key: 'age',
        },
        {
          title: '电话',
          slot: 'tel',
          key: 'tel',
        },
        {
          title: '邮箱',
          slot: 'email',
          key: 'email',
        },
        {
          title: '权限',
          slot: 'access',
          key: 'access',
        },
      ];
      ctx.body = {
        success: true,
        result: resultData
      };
    })
  }
};
